﻿using System.Text.RegularExpressions;

namespace TianlalaHSB.Utils;

public static partial class Number
{
    /// <summary>
    /// 只允许输入数字和小数点
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static bool IsNumber(string text)
    {
        return !string.IsNullOrEmpty(text) && MyRegex().IsMatch(text);
    }

    /// <summary>
    /// 只允许输入数字
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    public static bool IsInteger(string text)
    {
        if (string.IsNullOrEmpty(text)) return false;
        var regex = MyRegex1();
        return regex.IsMatch(text);
    }

    [GeneratedRegex("^[0-9]+(\\.[0-9]+)?$")]
    private static partial Regex MyRegex();

    [GeneratedRegex("^[0-9]+$")]
    private static partial Regex MyRegex1();
}